﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;
using System.Management;
using Microsoft.Win32;
using log4net;
namespace DSC
{
    public static class Util
    {
        private static readonly ILog _logger = LogManager.GetLogger("Util.cs");
        /// <summary>
        /// Gets the MAC address of the current PC.
        /// </summary>
        /// <returns></returns>
        public static string GetMacAddress()
        {
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            string MACAddress = String.Empty;
            foreach (ManagementObject mo in moc)
            {
                if (MACAddress == String.Empty)  // only return MAC Address from first card
                {
                    if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
                }
                mo.Dispose();
            }

            MACAddress = MACAddress.Replace(":", "");
            return MACAddress;
        }
        public static bool setLicense(string license,out string message)
        {
            try
            {
                RegistryKey rkAfit = Registry.CurrentUser.OpenSubKey("SOFTWARE", true).CreateSubKey("Afit Group", RegistryKeyPermissionCheck.ReadWriteSubTree);
                rkAfit.SetValue("license", license);
                rkAfit.Close();
                message = "";
                return true;
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
                message = ex.Message;
                return false;
            }
        }
        public static string getLicense()
        {
            try
            {
                RegistryKey rkAfit = Registry.CurrentUser.OpenSubKey("SOFTWARE", true).CreateSubKey("Afit Group", RegistryKeyPermissionCheck.ReadWriteSubTree);
                string license = (string)rkAfit.GetValue("license");
                rkAfit.Close();
                return license;
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
                return "";
            }
        }
    }
}
